package com.zm.boss.service.controller;

import com.zm.boss.service.common.anno.Title;
import com.zm.boss.service.common.enums.ErrorCodeEnum;
import com.zm.boss.service.exception.BizException;
import com.zm.boss.service.response.*;
import com.zm.boss.service.service.BaseServiceUp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author hacker
 */
@Slf4j
public abstract class BaseController<T,R> {
    public abstract BaseServiceUp getService();
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result<?> save(@RequestParam(value = "params") String params){
        Result<?> result = null;
        try{
            result = getService().save(params) > 0 ? Result.ofSuccess() :
                    Result.ofFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorCode(),
                            ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorMsg());
        }catch (BizException e){
            log.error("保存异常：[{}]",e);
            result = Result.ofFail(e.getCode(), e.getMsg());
        }catch (Exception e){
            log.error("系统异常：[{}]", e);
            result = Result.ofFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorCode(),
                    ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorMsg());
        }
        return result;
    }
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result<?> update(@RequestParam(value = "params") String params){
        Result<?> result = null;
        try{
            result = getService().update(params) > 0 ? Result.ofSuccess() :
                    Result.ofFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorCode(),
                            ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorMsg());
        }catch (BizException e){
            log.error("更新异常：[{}]",e);
            result = Result.ofFail(e.getCode(), e.getMsg());
        }catch (Exception e){
            log.error("系统异常：[{}]", e);
            result = Result.ofFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorCode(),
                    ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorMsg());
        }
        return result;
    }
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result<?> delete(@RequestParam(value = "params") String params){
        Result<?> result = null;
        try{
            result = getService().delete(params) > 0 ? Result.ofSuccess() :
                    Result.ofFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorCode(),
                            ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorMsg());
        }catch (BizException e){
            log.error(" 删除异常：[{}]",e);
            result = Result.ofFail(e.getCode(), e.getMsg());
        }catch (Exception e){
            log.error("系统异常：[{}]", e);
            result = Result.ofFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorCode(),
                    ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorMsg());
        }
        return result;
    }
    @RequestMapping(value = "/delete/model", method = RequestMethod.DELETE)
    public Result<?> delete(T params){
        Result<?> result = null;
        try{
            result = getService().delete(params) > 0 ? Result.ofSuccess() :
                    Result.ofFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorCode(),
                            ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorMsg());
        }catch (BizException e){
            log.error(" 删除异常：[{}]",e);
            result = Result.ofFail(e.getCode(), e.getMsg());
        }catch (Exception e){
            log.error("系统异常：[{}]", e);
            result = Result.ofFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorCode(),
                    ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorMsg());
        }
        return result;
    }
    @RequestMapping(value = "/find/condition", method = RequestMethod.GET)
    public Result<?> findById(@RequestParam(value = "params") String params){
        Result<?> result = null;
        try{
            result = Result.ofSuccess(getService().findById(params));
        }catch (BizException e){
            log.error("更新异常：[{}]",e);
            result = Result.ofFail(e.getCode(), e.getMsg());
        }catch (Exception e){
            log.error("系统异常：[{}]", e);
            result = Result.ofFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorCode(),
                    ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorMsg());
        }
        return result;
    }
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public PageResult<PaginationRespTemplate<R>> query(@RequestParam(value = "params") String params){
        PageResult<PaginationRespTemplate<R>> result = null;
        try{
            PaginationRespTemplate<R> zmProductModelList = new PaginationRespTemplate<>();
            zmProductModelList = getService().queryPage(params, zmProductModelList,getTitle(), null, null);
            result = PageResult.ofSuccess(zmProductModelList);
        }catch (BizException e){
            log.error("查询（分页）异常：[{}]",e);
            result = PageResult.ofFail(e.getCode(), e.getMsg());
        }catch (Exception e){
            log.error("系统异常：[{}]", e);
            result = PageResult.ofFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorCode(),
                    ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorMsg());
        }
        return result;
    }
    @RequestMapping(value = "/save/model", method = RequestMethod.POST)
    public Result<?> save(T params){
        Result<?> result = null;
        try{
            result = getService().save(params) > 0 ? Result.ofSuccess() :
                    Result.ofFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorCode(),
                            ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorMsg());
        }catch (BizException e){
            log.error("保存异常：[{}]",e);
            result = Result.ofFail(e.getCode(), e.getMsg());
        }catch (Exception e){
            log.error("系统异常：[{}]", e);
            result = Result.ofFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorCode(),
                    ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorMsg());
        }
        return result;
    }
    @RequestMapping(value = "/update/model", method = RequestMethod.PUT)
    public Result<?> update(T params){
        Result<?> result = null;
        try{
            result = getService().update(params) > 0 ? Result.ofSuccess() :
                    Result.ofFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorCode(),
                            ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorMsg());
        }catch (BizException e){
            log.error("更新异常：[{}]",e);
            result = Result.ofFail(e.getCode(), e.getMsg());
        }catch (Exception e){
            log.error("系统异常：[{}]", e);
            result = Result.ofFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorCode(),
                    ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorMsg());
        }
        return result;
    }
    @RequestMapping(value = "/find/model", method = RequestMethod.GET)
    public Result<?> findById(T params){
        Result<?> result = null;
        try{
            result = Result.ofSuccess(getService().findById(params));
        }catch (BizException e){
            log.error("更新异常：[{}]",e);
            result = Result.ofFail(e.getCode(), e.getMsg());
        }catch (Exception e){
            log.error("系统异常：[{}]", e);
            result = Result.ofFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorCode(),
                    ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorMsg());
        }
        return result;
    }
    @RequestMapping(value = "/query/model", method = RequestMethod.GET)
    public PageResult<PaginationRespTemplate<R>> query(T params){
        PageResult<PaginationRespTemplate<R>> result = null;
        try{
            PaginationRespTemplate<R> zmProductModelList = new PaginationRespTemplate<>();
            zmProductModelList = getService().queryPage(params, zmProductModelList,getTitle(), this::getHeader, this::getAttr);
            result = PageResult.ofSuccess(zmProductModelList);
        }catch (BizException e){
            log.error("查询（分页）异常：[{}]",e);
            result = PageResult.ofFail(e.getCode(), e.getMsg());
        }catch (Exception e){
            log.error("系统异常：[{}]", e);
            result = PageResult.ofFail(ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorCode(),
                    ErrorCodeEnum.SYSTEM_EXCEPTION.getErrorMsg());
        }
        return result;
    }

    /**
     * 获取excel的表头
     * @return Supplier<Header[]>
     */
    public abstract Supplier<Header[]> getHeader();

    /**
     * 获取额外的Attr
     * @return List<Attr>
     */
    public abstract Function<Object, List<Attr>> getAttr(Object obj);
    private String getTitle(){
        Title title = this.getClass().getAnnotation(Title.class); // 取得指定注释
        if(title != null) {
            return title.value();
        }
        return "未设置标题";
    }
}
